package com.ssm.demo.service.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ssm.demo.service.mapper.StudentMapper;
import com.ssm.demo.service.model.po.StudentPO;
import com.ssm.demo.service.view.vo.StudentReqVO;
import com.ssm.demo.service.view.vo.StudentVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Olsond
 * @date 2023/11/5
 * @description
 */
@Service
@RequiredArgsConstructor(onConstructor_= @Autowired)
public class StudentService implements IStudentService {

    public final StudentMapper studentMapper;

    @Transactional
    public void saveStudent(StudentVO studentVO) {
        StudentPO studentPO = new StudentPO();
        BeanUtils.copyProperties(studentVO, studentPO);
        if (!StringUtils.hasText(studentVO.getStudentNo())) {
            studentPO.setStudentNo(String.valueOf(System.currentTimeMillis()));
        }
        Date nowDate = new Date();
        studentPO.setCreateTime(nowDate);
        studentPO.setUpdateTime(nowDate);
        studentPO.setActive(1);
        studentMapper.insert(studentPO);
    }

    public void deleteStudent(String studentNo) {
        // 判断是否传入学生学号
        if(!StringUtils.hasText(studentNo)) {
            return;
        }
        LambdaUpdateWrapper<StudentPO> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(StudentPO::getStudentNo, studentNo);
        studentMapper.delete(lambdaUpdateWrapper);
    }

    @Override
    public void updateStudent(StudentVO studentVO) {
        // 判断是否传入学生学号
        if(!StringUtils.hasText(studentVO.getStudentNo())) {
            return;
        }
        StudentPO studentPO = new StudentPO();
        studentPO.setName(studentVO.getName());
        studentPO.setAge(studentVO.getAge());
        LambdaUpdateWrapper<StudentPO> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(StudentPO::getStudentNo, studentVO.getStudentNo());
        studentMapper.update(studentPO, lambdaUpdateWrapper);
    }

    @Override
    public List<StudentVO> queryStudent(StudentReqVO reqVO) {
        LambdaQueryWrapper<StudentPO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StringUtils.hasText(reqVO.getStudentNo()), StudentPO::getStudentNo, reqVO.getStudentNo())
                .like(StringUtils.hasText(reqVO.getName()), StudentPO::getName, reqVO.getName())
                .eq(null != reqVO.getSexCode(), StudentPO::getSexCode, reqVO.getSexCode())
                .eq(null != reqVO.getAge(), StudentPO::getAge, reqVO.getAge())
                .eq(StudentPO::getActive, 1);
        List<StudentPO> resultList = studentMapper.selectList(lambdaQueryWrapper);
        if (CollectionUtils.isEmpty(resultList)) {
            return null;
        }
        // 转换结果为vo对象
        return resultList.stream().map(po -> {
            StudentVO studentVO = new StudentVO();
            BeanUtils.copyProperties(po, studentVO);
            return studentVO;
        }).collect(Collectors.toList());
    }

}
